package com.project.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 后端项目配置类
 *
 * @author Leo高洋
 * @create 2023-01-16 23:42
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
    /**
     * 放开全部端口，解决跨域问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }

    /**
     * 资源拦截，映射资源地址，本地路径file，项目路径classpath
     * 注：映射千万不能加空格
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc/**").addResourceLocations("classpath:/smartDoc/doc/");
        // 用户头像图片映射
        registry.addResourceHandler("/image/userAvatar/**").addResourceLocations("classpath:/static/image/");
        // 资产设备采购合同路径映射
        registry.addResourceHandler("/contract/**").addResourceLocations("file:E:\\2023GraduationDesign\\uploadFile\\AssetPurchaseContract\\");
    }
}
